<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Article
{

	var $db;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}

	/**
	 * 获取分类
	 *
	 * @return array
	 */
	function get_category( $count_ar_no = false )
	{
		$category = cache_read( 'article_category' );
		if( $count_ar_no )
		{
			foreach( $category as $key => $value )
			{
				$category[$key]['article_no'] = $this->db->count( DB_PRE.'article', array( 'cid' => $value['id'], 'draft' => 0 ) );
			}
		}
		return $category;
	}

	/**
	 * 获取专辑列表
	 *
	 */
	function get_special_list()
	{
		$sql = "SELECT `id`, `name` FROM `".DB_PRE."special`";
		return $this->db->query( $sql );
	}
	
	/**
	 * 获取来源列表
	 *
	 */
	function get_from_list()
	{
		$data = file_get_contents( SUN_CACHE.'from.txt' );
		$from_array = explode( '|', $data );
		$from = array();
		foreach( $from_array as $v )
		{
			if( !empty( $v ) )
			{
				$from[] = $v;
			}
		}
		return $from;
	}
	
	/**
	 * 获取作者列表
	 *
	 * @return unknown
	 */
	function get_author_list()
	{
		$data = file_get_contents( SUN_CACHE.'author.txt' );
		$author_array = explode( '|', $data );
		$author = array();
		foreach( $author_array as $v )
		{
			if( !empty( $v ) )
			{
				$author[] = $v;
			}
		}
		return $author;
	}

	function get_area( $type, $province = '' )
	{
		if( $type == 'province' ) return $this->db->get_where( DB_PRE.'area', array( 'pid' => 0 ) );
		elseif( !empty( $province ) ) return $this->db->get_where( DB_PRE.'area', array( 'pid' => $province ) );
	}
	
	/**
	 * 对文章进行处理并插入到数据库
	 *
	 * @param array $datas
	 * @return bool
	 */
	function add_article( $datas )
	{
		global $SUNCMS;
		$datas['get_first_pic'] = isset( $datas['get_first_pic'] )?1:0;
		$datas['down_pic'] = isset( $datas['down_pic'] )?1:0;
		$datas['index_top'] = isset( $datas['index_top'] )?1:0;
		$datas['category_top'] = isset( $datas['category_top'] )?1:0;
		$datas['draft'] = isset( $datas['draft'] )?1:0;
		$datas['flash'] = isset( $datas['flash'] )?1:0;
		$datas['best'] = isset( $datas['best'] )?1:0;
		$datas['recommend'] = isset( $datas['recommend'] )?1:0;
		$datas['validate'] = isset( $datas['validate'] )?1:0;
		$datas['post_time'] = empty( $datas['post_time'] )?TIME:strtotime( $datas['post_time'] );
		$datas['ip'] = IP;
		$get_first_pic = ( empty( $datas['thumb_pic'] ) && $datas['get_first_pic'] )?true:false;
		//如果description为空，自动提取description
		if( empty( $datas['description'] ) )
		{
			$contents = preg_replace( '/\[-page-\]|\[-\/page-\]/', '', $datas['content'] );
			$contents = str_replace( '\n', '', $contents );
			$contents = str_replace( '\r', '', $contents );
			$contents = strip_tags( $contents );
			$datas['description'] = my_substr( $contents, 300 );
			unset( $contents );
		}
		//如果keywords为空，则自动提取keywords
		if( empty( $datas['keywords'] ) )
		{
			$datas['keywords'] = $this->get_tags( $datas['title'], $datas['description'] );
		}
		//如果tags为空，则将keywords转化为tags
		if( empty( $datas['tags'] ) )
		{
			$datas['tags'] = $datas['keywords'];
		}
		//下载非站内图片以及提取内容中第一个图片为缩略图
		$result = $this->get_pic( $datas['content'], $datas['down_pic'], $get_first_pic );
		$get_first_pic && $datas['thumb_pic'] = $result['thumb_pic'];
		$datas['content'] = $result['content'];
		$datas['picture'] = $result['picture'];
		//对限制词进行处理
		$datas['content'] = $this->add_banwords( $datas['content'] );
		//判断是否需要分页
		if( strstr( $datas['content'], '[-page-]' ) )
		{
			$content_arr = $this->explode_content( $datas['content'] );
			$datas['page_no'] = count( $content_arr );
		}
		else 
		{
			$content_arr = array( array( 'title' => '', 'content' => $datas['content'] ) );
			$datas['page_no'] = 1;
		}
		//销毁不用的变量
		unset( $datas['content'], $datas['get_first_pic'], $datas['down_pic'], $datas['first_page_title'] );
		//判断文章基本数据是否插入到数据库
		if( !$this->db->insert( DB_PRE.'article', escape( strim( $datas ) ) ) )
		{
			return false;
		}
		$id = $this->db->insert_id();
		//判断内容是否插入到数据库
		if( !$this->insert_article_content( $content_arr, $id ) )
		{
			$this->del_one_article( $id );
			return false;
		}
		//处理tags
		$this->add_update_tags( $datas['tags'], $id );
		//处理special
		if( !empty( $special ) )
		{
			$this->update_special_aids( $id, strim( $datas['sid'] ) );
		}
		//处理财富
		$sql_mb = "UPDATE `".DB_PRE."memberdata` SET `integral` = `integral`+{$SUNCMS['addarticle_integral']}, `money` = `money`+{$SUNCMS['addarticle_money']} WHERE `uid` = {$datas['uid']}";
		$this->db->execute( $sql_mb );
		unset( $datas );
		return $id;
	}

	/**
	 * 获取tags
	 *
	 * @param string $content
	 */
	function get_tags( $title, $content )
	{
		include( SUNCMS.'suncore/split.class.php' );
		$this->split = new Split();
		$keywords = '';
		$kws = '';
		$kwsArray = array();
		$titleTags = explode( ' ', @$this->split->GetIndexText( $title ) );
		$contentTags = explode( ' ', @$this->split->GetIndexText( $content ) );
		if( is_array( $titleTags ) && is_array( $contentTags ) )
		{
			foreach( $titleTags as $v )
			{
				if( strlen( $kws.$v ) >= 30 )
				{
					break;
				}
				else
				{
					$kws .= $v.',';
					$kwsArray[] = $v;
				}
			}
			foreach( $contentTags as $v )
			{
				if( strlen( $kws.$v ) >= 30 )
				{
					break;
				}
				else if( !in_array( $v, $titleTags ) )
				{
					$kws .= $v.',';
					$kwsArray[] = $v;
				}
			}
		}
		$this->split->Clear();
		$this->split = null;
		$kwsNo = count( $kwsArray );
		foreach( $kwsArray as $k => $v )
		{
			$keywords .= ( $k+1 == $kwsNo )?$v:$v.' ';
		}
		return $keywords;
	}
	
	/**
	 * 下载非站内图片并加上水印
	 *
	 * @param string $str
	 * @return string
	 */
	function get_pic( $str, $down_pic, $get_first_pic = false )
	{
		global $SUNCMS;
		preg_match_all( "/http:\/\/([^ '\"<>]+)\.(gif|jpg|png)/is", $str, $array );
		if( empty( $array[0] ) )
		{
			return array( 'thumb_pic' => '', 'content' => $str, 'picture' => '' );
		}
		include( SUNCMS.'suncore/images.class.php' );
		$this->m_images = new Images();
		$urls = array();
		$thumb_pic = '';
		$content = '';
		$picture = '';
		foreach( $array[0] as $v )
		{
			if( !strstr( SITE_URL, $v ) )
			{
				$urls[] = $v;
			}
		}
		if( $down_pic || $get_first_pic  )
		{
			foreach( $urls AS $key => $value)
			{
				$name_ext = basename( $value );
				$name_exts = explode( '.', $name_ext );
				$name = md5( microtime( true ).$name_exts[0] ).'.'.$name_exts[1];
				$validate = false;
				if( @copy( $value, SUNCMS.'uploads/temp/'.$name ) )
				{
					$validate = true;
					chmod( SUNCMS.'uploads/temp/'.$name, 0777 );
				}
				elseif( $img = file_get_contents( $value ) )
				{
					$validate = true;
					file_put_contents( SUNCMS.'uploads/temp/'.$name, $img );
					chmod( SUNCMS.'uploads/temp/'.$name, 0777 );
				}
				if( $get_first_pic && $key == 0 )
				{
					$thumb_pic = ( $this->m_images->resize_images( $name ) )?$name:'';
					if( !$down_pic )
					{
						return array( 'thumb_pic' => $thumb_pic, 'content' => $str, 'picture' => $value );
					}
				}
				//加水印
				$validate && $config['watermark'] && $this->m_images->watermark_img( $name, $SUNCMS['watermark_img'] );
				//图片路径替换
				if( $validate )
				{
					$new_url = SITE_URL.'uploads/images/'.$name;
					$key == 0 && $picture = $new_url;
					$str = str_replace( $value, $new_url, $str );
				}
				unset( $name_ext, $name_exts, $name, $validate );
			}
		}
		else 
		{
			$picture = isset( $urls[0] )?$urls[0]:'';
		}
		return array( 'thumb_pic' => $thumb_pic, 'content' => $str, 'picture' => $picture );
	}
	
	/**
	 * 进行分页
	 *
	 * @param string $string
	 * @param int $type
	 * @return array
	 */
	function explode_content( $string )
	{
		$contents = explode( '[-page-]', new_stripslashes( $string ) );
		$datas = array();
		$i = 0;
		foreach( $contents AS $key => $value)
		{
			$value = strim( $value );
			if( !empty( $value ) && $value != '[-/page-]'  ) 
			{
				if( strstr( $value, '[-/page-]' ) )
				{
					$tc = explode( '[-/page-]', $value );
					$datas[$i]['content'] = $tc[1];
					$datas[$i]['title'] = strim($tc[0]);
				}
				else
				{
					$datas[$i]['content'] = str_replace( "[-/page-]", '', $value );
					$datas[$i]['title'] ='';
				}
				$i++;
			}
		}
		return new_addslashes( $datas );
	}
	
	/**
	 * 对关键词加链接
	 *
	 * @param string $str
	 */
	function add_banwords( $str )
	{
		$content = $str;
		$config = cache_read( 'blog', 'banword' );
		foreach( $config as $k => $v )
		{
			$content = str_replace( "{$v['name']}", '<a href=\''.$v['url'].'\' target=_bank>'.$v['name'].'</a>', $content );
		}
		return $content;
	}
	
	/**
	 * 添加文章内容到数据库
	 *
	 * @param array $content
	 * @param int $id
	 */
	function insert_article_content( $content, $id, $page = 1 )
	{
		$i = 0;
		foreach( $content as $k => $v )
		{
			$v['content'] = str_replace( '"', "'", $v['content'] );
			$now_page = $page+$k;
			$datas = array( 'aid' => $id, 'title' => $v['title'], 'content' => $v['content'], 'page' => $now_page );
			!$this->db->insert( DB_PRE.'articlecontent', escape( $datas ) ) && $i = 1;
			unset( $v, $now_page, $datas );
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 将tags更新/插入到数据库
	 *
	 * @param string $tags
	 * @param int $id
	 */
	function add_update_tags( $tags, $id, $update = false )
	{
		if( $update )
		{
			$this->db->delete( DB_PRE.'tags', array( 'aid' => $id ) );
		}
		$tags = explode( ' ', $tags );
		$i = 0;
		foreach( $tags as $v )
		{
			$sql = "SELECT `no` FROM `".DB_PRE."taginfo` WHERE `name` = '$v'";
			$taginfo = $this->db->query( $sql, true );
			!empty( $taginfo )?$this->db->update( DB_PRE.'taginfo', array( 'no' => $taginfo['no']+1 ), array( 'name' => $v ) ):$this->db->insert( DB_PRE.'taginfo', array( 'name' => $v, 'no' => 1 ) );
			$data = array( 'name' => $v, 'aid' => $id );
			$this->db->insert( DB_PRE.'tags', $data );
			unset( $data );
		}
		return;
	}

	/**
	 * 更新专题文章数据
	 *
	 * @param int $aid
	 * @param strimg $sids
	 */
	function update_special_aids( $aid, $sids )
	{
		$sids = explode( ' ', $sids );
		foreach( $sids as $v )
		{
			$sql = "SELECT `aids` FROM `".DB_PRE."special` WHERE `id` = '$v'";
			$result = $this->db->query( $sql, true );
			$this->db->update( DB_PRE.'special', array( 'aids' => $result['aids'].$aid." " ), array( 'id' => $v ) );
			unset( $result );
		}
		return;
	}

	/**
	 * 获取文章数
	 *
	 * @param string $type
	 */
	function get_article_no( $type, $id = 0 )
	{
		switch( $type )
		{
			case 'col':
				$where = array( 'cid' => $id, 'draft' => 0 );
				break;
			case 'draft':
				$where = array( 'draft' => 1 );
				break;
			case 'vali':
				$where = array( 'validate' => 0 );
				break;
			default:
				$where = array( 'draft' => 0 );
		}
		$result = $this->db->count( DB_PRE.'article', $where );
		return $result;
	}
	
	/**
	 * 获取文章列表
	 *
	 * @param int $start
	 * @param string $type
	 * @return array
	 */
	function get_article_list( $start, $type, $id )
	{
		switch( $type )
		{
			case 'draft':
				$where = "WHERE `a`.`draft` = 1";
				break;
			case 'all':
				$where = "WHERE `a`.`draft` = 0";
				break;
			case 'vali':
				$where = "WHERE `a`.`validate` = 0";
				break;
			case 'col':
				$where = "WHERE `a`.`cid` = $id AND `a`.`draft` = 0";
				break;
		}
		$sql = "SELECT `a`.`id`, `a`.`title`, `a`.`small_title`, `a`.`page_no`, `a`.`recommend`, `a`.`title_color`, `a`.`post_time`, `b`.`name` AS `category`, `c`.`username` AS `creater` FROM `".DB_PRE."article` as `a` LEFT JOIN `".DB_PRE."category` as `b` ON `b`.`id`= `a`.`cid` AND `b`.`type` = 'article' LEFT JOIN `".DB_PRE."member` as `c` ON `c`.`id`= `a`.`uid` ".$where." ORDER BY `a`.`post_time` DESC LIMIT $start, 15";
		$result = $this->db->query( $sql );
		if( !empty( $result ) )
		{
			foreach( $result as $key => $value )
			{
				$result[$key]['title'] = empty( $result[$key]['small_title'] )?$result[$key]['title']:$result[$key]['small_title'];
				$result[$key]['post_time'] = date( "y-m-d H:i", $result[$key]['post_time'] );
			}
			return $result;
		}
		return array();
	}

	function get_article_info( $id, $page_info = false )
	{
		$sql = "SELECT `title`, `page_no` FROM `".DB_PRE."article` WHERE `id` = $id";
		$datas = $this->db->query( $sql, true );
		if( $page_info )
		{
			$sql_page = "SELECT `title`, `page` FROM `".DB_PRE."articlecontent` WHERE `aid` = $id ORDER BY `page` ASC";
			$page_info = $this->db->query( $sql_page );
			$datas['page'] = $page_info;
		}
		return $datas;
	}

	function add_more( $id, $datas )
	{
		$datas['down_pic'] = isset( $datas['down_pic'] )?1:0;
		//下载非站内图片以及提取内容中第一个图片为缩略图
		$result = $this->get_pic( $datas['content'], $datas['down_pic'], false );
		$datas['content'] = $result['content'];
		//对限制词进行处理
		$datas['content'] = $this->add_banwords( $datas['content'] );
		//判断是否需要分页
		if( strstr( $datas['content'], '[-page-]' ) )
		{
			$content_arr = $this->explode_content( $datas['content'] );
			$page_no = count( $content_arr );
		}
		else 
		{
			$content_arr = array( array( 'title' => '', 'content' => $datas['content'] ) );
			$page_no = 1;
		}
		if( $this->insert_article_content( $content_arr, $id, $datas['page_no']+1 ) )
		{
			$this->db->update( DB_PRE.'article', array( 'page_no' => $datas['page_no']+$page_no ), array( 'id' => $id ) );
			return true;
		}
		else
		{
			return false;
		}
	}

	function get_edit_article( $id, $page, $type = false )
	{
		if ( $type )
		{
			$sql_info = "SELECT `cid`, `sid`, `title`, `small_title`, `title_color`, `tags`, `keywords`,`author`, `from`, `from_url`, `index_top`, `category_top`, `recommend`, `description`, `if_comment`, `flash`, `target`, `picture`, `province`, `city`, `best`, `recommend`, `money`, `post_time`, `validate`, `password`, `thumb_pic`, `draft` FROM `".DB_PRE."article` WHERE `id` = '$id'";
			$datas =  $this->db->query( $sql_info, true );
			$content = '';
			if( $page == 0 )
			{
				$sql = "SELECT `title`, `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' ORDER BY `page` ASC";
				$result = $this->db->query( $sql );
				$page_no = count( $result );
				foreach( $result as $k => $v )
				{
					$content .= ( $k == 0 && $v['title'] == '' )?$v['content']:'[-page-]'.$v['title'].'[-/page-]'.$v['content'];
				}
			}
			else if( $page == 1 )
			{
				$sql = "SELECT `title`, `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' AND `page` = '1'";
				$result = $this->db->query( $sql, true );
				$content .= $result['title'] == ''?$result['content']:'[-page-]'.$result['title'].'[-/page-]'.$result['content'];
			}
			$datas['content'] = $content;
			$datas['post_time'] = date( 'Y-m-d H:i:s', $datas['post_time'] );
			return $datas;
		}
		else
		{
			$sql = "SELECT `title`, `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = '$id' AND `page` = '$page'";
			$result = $this->db->query( $sql, true );
			$content = $result['title'] == ''?$result['content']:'[-page-]'.$result['title'].'[-/page-]'.$result['content'];
			return $content;
		}
	}

	function update_article( $id, $page, $datas, $type )
	{
		$datas['down_pic'] = isset( $datas['down_pic'] )?1:0;
		if( $type )
		{
			$datas['get_first_pic'] = isset( $datas['get_first_pic'] )?1:0;
			$datas['index_top'] = isset( $datas['index_top'] )?1:0;
			$datas['category_top'] = isset( $datas['category_top'] )?1:0;
			$datas['draft'] = isset( $datas['draft'] )?1:0;
			$datas['flash'] = isset( $datas['flash'] )?1:0;
			$datas['best'] = isset( $datas['best'] )?1:0;
			$datas['recommend'] = isset( $datas['recommend'] )?1:0;
			$datas['validate'] = isset( $datas['validate'] )?1:0;
			$datas['post_time'] = empty( $datas['post_time'] )?TIME:strtotime( $datas['post_time'] );
			$datas['edit_time'] = TIME;
			//如果上传了新缩略图并且原缩略图不为空
			if(  !empty( $datas['thumb_pic'] ) && !empty( $datas['old_thumb_pic'] ) && ( $datas['old_thumb_pic'] != $datas['thumb_pic'] ) )
			{
				@unlink( SUNCMS.'uploads/thumb/'.$datas['old_thumb_pic'] );
			}
			$get_first_pic = ( $datas['thumb_pic'] == '' && $datas['get_first_pic'] )?true:false;
			//如果description为空，自动提取description
			if( empty( $datas['description'] ) )
			{
				$contents = preg_replace( '/\[-page-\]|\[-\/page-\]/', '', $datas['content'] );
				$datas['description'] = my_substr( strip_tags( $contents ), 300 );
				unset( $contents );
			}
			//如果keywords为空，则自动提取keywords
			if( empty( $datas['keywords'] ) )
			{
				$datas['keywords'] = $this->get_tags( $datas['title'], $datas['description'] );
			}
			//如果tags为空，则将keywords转化为tags
			if( empty( $datas['tags'] ) )
			{
				$datas['tags'] = $datas['keywords'];
			}
			//下载非站内图片以及提取内容中第一个图片为缩略图
			$result = $this->get_pic( $datas['content'], $datas['down_pic'], $get_first_pic );
			$get_first_pic && $datas['thumb_pic'] = $result['thumb_pic'];
			$datas['content'] = $result['content'];
			$datas['picture'] = $datas['picture'] == ''?$result['picture']:'';
			//对限制词进行处理
			$datas['content'] = $this->add_banwords( $datas['content'] );
			//判断是否需要分页
			if( strstr( $datas['content'], '[-page-]' ) )
			{
				$content_arr = $this->explode_content( $datas['content'] );
				$page_no = count( $content_arr );
				$page == 0?$datas['page_no'] = $page_no:$datas['page_no'] = $datas['page_no']+$page_no-1;
			}
			else 
			{
				$content_arr = array( array( 'title' => '', 'content' => $datas['content'] ) );
				$page_no = 1;
				$page == 0 && $datas['page_no'] = 1;
			}
			//销毁不用的变量
			unset( $datas['content'], $datas['get_first_pic'], $datas['down_pic'], $datas['first_page_title'], $datas['old_thumb_pic'] );
			//判断文章基本数据是否插入到数据库
			if( !$this->db->update( DB_PRE.'article', escape( strim( $datas ) ), array( 'id' => $id ) ) )
			{
				return false;
			}
			if( $page == 0 )
			{
				$this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id ) );
			}
			else 
			{
				$this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id, 'page' => 1 ) );
				$this->update_other_page( $id, 1, $page_no );
			}
			//判断内容是否插入到数据库
			if( !$this->insert_article_content( $content_arr, $id ) )
			{
				return false;
			}
		}
		else 
		{
			$content = $datas['content'];
			//下载非站内图片以及提取内容中第一个图片为缩略图
			$result = $this->get_pic( $content, $datas['down_pic'], false );
			$content = $result['content'];
			//判断是否需要分页
			if( strstr( $datas['content'], '[-page-]' ) )
			{
				$content_arr = $this->explode_content( $content );
				$page_no = count( $content_arr );
			}
			else 
			{
				$content_arr = array( array( 'title' => '', 'content' => $content ) );
				$page_no = 1;
			}
			$this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id, 'page' => $page ) );
			//是否需要更新内容其他页页数
			$page_no > 1 && $this->update_other_page( $id, $page, $page_no-1 );
			if( !$this->insert_article_content( $content_arr, $id, $page ) )
			{
				return false;
			}
			$this->db->update( DB_PRE.'article', array( 'page_no' => $datas['page_no']+$page_no-1 ), array( 'id' => $id ) );
		}
		return true;
	}

	function update_other_page( $id, $page, $page_no )
	{
		$sql = "UPDATE `".DB_PRE."articlecontent` SET `page` = `page`+$page_no WHERE `aid` = $id AND `page` > $page";
		$this->db->execute( $sql );
		return;
	}

	function del_page( $id, $page )
	{
		$dwl = $this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id, 'page' => $page ) );
		$sql_c = "UPDATE `".DB_PRE."articlecontent` SET `page` = `page`-1 WHERE `aid` = '$id' AND `page` > '$page'";
		$update_c = $this->db->execute( $sql_c );
		if( $dwl && $update_c )
		{
			$sql_i = "UPDATE `".DB_PRE."article` SET `page_no` = `page_no`-1 WHERE `id` = $id";
			$this->db->execute( $sql_i );
		}
		return ( $dwl && $update_c )?true:false;
	}

	function copy( $ids, $cids )
	{
		$ids = explode( '_', $ids );
		$cids = explode( '_', $cids );
		foreach( $ids as $v )
		{
			$sql_info = "SELECT `cid`, `uid`, `page_no`, `sid`, `title`, `small_title`, `title_color`, `tags`, `keywords`,`author`, `from`, `from_url`, `index_top`, `category_top`, `recommend`, `description`, `if_comment`, `info`, `flash`, `target`, `picture`, `province`, `city`, `best`, `recommend`, `money`, `post_time`, `validate`, `password`, `thumb_pic`, `draft` FROM `".DB_PRE."article` WHERE `id` = '$v'";
			$article_info = $this->db->query( $sql_info, true );
			$sql_cont = "SELECT `title`, `content` FROM `".DB_PRE."articlecontent` WHERE `aid` = $v ORDER BY `page` ASC";
			$article_cont = $this->db->query( $sql_cont );
			$infos = $article_info['info'];
			unset( $article_info['info'] );
			$copy_ids = array();
			foreach( $cids as $v2 )
			{
				if( $article_info['cid'] != $v2 )
				{
					$article_info['cid'] = $v2;
					if( !$this->db->insert( DB_PRE.'article', $article_info ) )
					{
						return false;
					}
					$id = $this->db->insert_id();
					if( !$this->insert_article_content( $article_cont, $id ) )
					{
						return false;
					}
					$copy_ids[] = $id;
					unset( $id );
				}
			}
			$infos = unserialize( $infos );
			if( $infos == '' || ( $infos != '' && !isset( $infos['copy'] ) ) )
			{
				$infos['copy'] = $copy_ids;
			}
			else 
			{
				$infos['copy'] = array_merge( $infos['copy'], $copy_ids );
			}
			$infos = serialize( $infos );
			$this->db->update( DB_PRE.'article', array( 'info' => $infos ), array( 'id' => $v ) );
			unset( $sql_info, $article_info, $sql_cont, $article_cont, $infos, $copy_ids );
		}
		return true;
	}

	function move( $ids, $cid )
	{
		$ids = explode( '_', $ids );
		foreach( $ids as $v )
		{
			if( !$this->db->update( DB_PRE.'article', array( 'cid' => $cid ), array( 'id' => $v ) ) )
			{
				return false;
			}
		}
		return true;
	}

	function del_article( $ids )
	{
		$ids = explode( '_', $ids );
		foreach( $ids as $v )
		{
			if( !$this->del_one_article( $v ) )
			{
				return false;
			}
		}
		return true;
	}
	
	function del_one_article( $id )
	{
		global $SUNCMS;
		$sql = "SELECT `uid`, `picture`, `thumb_pic`, `info` FROM `".DB_PRE."article` WHERE `id` = $id";
		$article_info = $this->db->query( $sql, true );
		$infos = unserialize( $article_info['info'] );
		if( $infos != '' && !isset( $infos['copy'] ) )
		{
			$article_info['thumb_pic'] != '' && @unlink( SUNCMS.'uploads/thumb/'.$article_info['thumb_pic'] );
			$article_info['picture'] != '' && @unlink( SUNCMS.'uploads/images/'.$article_info['picture'] );
		}
		$sql_mb = "UPDATE `".DB_PRE."memberdata` SET `integral` = `integral`-{$SUNCMS['delarticle_integral']}, `money` = `money`-{$SUNCMS['delarticle_money']} WHERE `uid` = {$article_info['uid']}";
		$this->db->execute( $sql_mb );
		$del_info = $this->db->delete( DB_PRE.'article', array( 'id' => $id ) );
		$sql_cont = $this->db->delete( DB_PRE.'articlecontent', array( 'aid' => $id ) );
		return ( $del_info && $sql_cont )?true:false;
	}

















}